package yunwo.cn.yzstation.base;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.autolayout.AutoLayoutActivity;

import java.util.Stack;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.view.activity.LoginActivity;


/**
 * Created by lao on 2017/8/23.
 */

public abstract class MyBaseActivity extends AutoLayoutActivity {
    private static Stack<Activity> listActivity = new Stack<Activity>();
    protected Typeface mTfLight;
    private TextView mTitlename;
    private ImageView mBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
        setContentView(getLayoutRsid());
        mTitlename = (TextView) findViewById(R.id.titlename);
        mBack = (ImageView) findViewById(R.id.back);
        if (mTitlename != null) {
            mTitlename.setText(settiltile());
        }
        // 将activity推入栈中
        listActivity.push(this);
        initback();
    }

    private void initback() {
        if (mBack != null) {
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从栈中移除当前activity
        if (listActivity.contains(this)) {
            listActivity.remove(this);
        }

    }





    protected static void finishAll() {
        int len = listActivity.size();
        for (int i = 0; i < len; i++) {
            Activity activity = listActivity.pop();
            activity.finish();
        }
    }

    protected static void finishother() {
        int len = listActivity.size();
        listActivity.remove(LoginActivity.class);
        for (int i = 0; i < len; i++) {
            Activity activity = listActivity.pop();
            activity.finish();
        }
    }

    protected abstract String settiltile();
    protected abstract int getLayoutRsid();

}
